﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.OMM.Configuration.Receptor;

namespace Sinacor.OMM.Configuration.Receptor
{
    /// <summary>
    /// Read de receptor configuration
    /// </summary>
    public class ReceptorConfigurationReader : BaseResourceAccess
    {

        private Int32 _companyId;

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public ReceptorConfigurationReader(Int32 companyId)
            : base(companyId)
		{
            _companyId = companyId;
		}

		#endregion

        #region Methods

        /// <summary>
        /// Read the receptor configuration
        /// </summary>
        /// <returns></returns>
        public OMM.Configuration.Receptor.ReceptorConfiguration GetConfiguration()
        {
            //
            Configuration.Receptor.ReceptorConfiguration receptorConfiguration = null;
            List<CommunicationChannel> _communicationChannels = null;
            Configuration.Repository.RepositoryConfigurationReader repositoryReader = new Sinacor.OMM.Configuration.Repository.RepositoryConfigurationReader(_companyId);
            String sqlCommand = String.Empty;

            //
            sqlCommand = @" SELECT COD_CANL_COMU,
                                   NOME_CANL_COMU,
                                   DESC_CANL_COMU,
                                   NOME_USUA_CONX,
                                   TXT_SENH_USUA_CONX,
                                   NOME_FUNC_SESS,
                                   COD_REPRIO_PRIN,
                                   COD_REPRIO_MSG_SIST,
                                   NOME_DRTO_ARQ_CTRL
                            FROM TBOCNFG_RCPT ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                _communicationChannels = this.FindAll<CommunicationChannel>(dbCommand,
                  reader =>
                  {
                      CommunicationChannel channel = new CommunicationChannel();

                      channel.Id = reader.GetValue<Int32>("COD_CANL_COMU");
                      channel.Name = reader.GetValue<String>("NOME_CANL_COMU");
                      channel.Description = reader.GetValue<String>("DESC_CANL_COMU");
                      channel.User = reader.GetValue<String>("NOME_USUA_CONX");
                      channel.Password = reader.GetValue<String>("TXT_SENH_USUA_CONX");
                      channel.Employee = reader.GetValue<String>("NOME_FUNC_SESS");
                      channel.ControllerFilePath = reader.GetValue<String>("NOME_DRTO_ARQ_CTRL");
                      channel.MessageMainRepository.Id = reader.GetValue<Int32>("COD_REPRIO_PRIN");
                      channel.SystemMessageRepository.Id = reader.GetValue<Int32?>("COD_REPRIO_MSG_SIST");

                      return channel;
                  });
            }

            foreach (CommunicationChannel item in _communicationChannels)
            {

                //Message main repository
                if (item.MessageMainRepository.Id.HasValue)
                {
                    item.MessageMainRepository = repositoryReader.GetConfiguration(item.MessageMainRepository.Id.Value);
                }
                else
                {
                    item.MessageMainRepository = null;
                }

                //System message repository
                if (item.SystemMessageRepository.Id.HasValue)
                {
                    item.SystemMessageRepository = repositoryReader.GetConfiguration(item.SystemMessageRepository.Id.Value);
                }
                else
                {
                    item.SystemMessageRepository = null;
                }
            }

            //
            if (_communicationChannels != null && 
                _communicationChannels.Count > 0)
            {
                receptorConfiguration = new ReceptorConfiguration();
                receptorConfiguration.CommunicationChannels = _communicationChannels;
            }

            return receptorConfiguration;
        }


        #endregion

    }
}
